import tkinter as tk
import generate_img
from PIL import Image, ImageTk


def window_init():
    game_frame.grid()
    title.grid(row=0, columnspan=4)
    canvas.grid(row=1,columnspan=4)
    blue_tooth_button.grid(row=2, column=0)
    winui_button.grid(row=2, column=1)
    quit_button.grid(row=2, column=3)
    ace_button.grid(row=2, column=2)


def show_bg(filename,canvas):
    global im
    image = Image.open(filename)  
    im = image.resize((800,480))
    im = ImageTk.PhotoImage(im)                  
    canvas.create_image(400,240,image = im,tag="img")      # 使用create_image将图片添加到Canvas组件中  

def send_img_by_ui(canvas):
    filename = generate_img.get_img_dir()
    if(filename != ''):
        filename = filename.replace("\\","/") 
        canvas.delete("img")
        show_bg(filename,canvas)
        generate_img.send_one_image(filename)  

if __name__ =='__main__':
    title = "A.C.E."
    master = tk.Tk()
    master.title("ACE")
    master.geometry("800x600")
    master.resizable(width=False, height=False)

    game_frame = tk.Frame(master)
    image = Image.open("img/bg.jpg")  
    im = image.resize((800,480))
    im = ImageTk.PhotoImage(im)
    canvas = tk.Canvas(game_frame,
                        width=800,
                        height=480)
                            
    canvas.create_image(400,240,image = im,tag="img")      # 使用create_image将图片添加到Canvas组件中  


    title = tk.Label(game_frame,
                    justify="center",
                    font="Times 25 bold",
                    fg="purple",
                    text=title)

    blue_tooth_button = tk.Button(game_frame,
                                justify="right",
                                compound="center",
                                font="Times",
                                text="蓝牙连接",
                                command=lambda: generate_img.blue_tooth_connect()    
    )

    ace_button = tk.Button(game_frame,
                        justify="left",
                        compound="center",
                        font="Times",
                        text="A.C.E",
                        command=lambda: generate_img.send_images_by_dir("img/ACE"))
                        
    winui_button = tk.Button(game_frame,
                        justify="left",
                        compound="center",
                        font="Times",
                        text="选择图片",
                        command=lambda: send_img_by_ui(canvas))

    quit_button = tk.Button(game_frame,
                            justify="left",
                            compound="center",
                            font="Times",
                            text="退出",
                            command=master.quit)
    window_init()
    master.mainloop()





